<?php
include_once("fckeditor/fckeditor.php");
if(isset($_REQUEST['noticia_id'])) {
	$newsString = "select title,body,status from noticias where noticia_id=".$_REQUEST['noticia_id'];
	$newsQuery = dbSelect($newsString) or die("Error en select ".$newsString." ".mysql_error());;
	$newsResult = mysql_fetch_array($newsQuery);
} else {
	$newsResult['title'] = $_REQUEST['title'];
	$newsResult['status'] = $_REQUEST['status'];
	$newsResult['body'] = $_REQUEST['FCKeditor1'];
}
?>

<script language="JavaScript">
function quitar_tag(tagid, index, content) {
	var tagdiv=document.getElementById("tagdiv");
	var combo=document.getElementById("tagcombo");
	
	var option = document.createElement('option');
	option.text = content;
	option.value = tagid;

	if(combo.length == 1) {
		combo.style.visibility = "";
	}
	
	var elOptOld = combo.options[index];
	try {
		combo.add(option, elOptOld); // standards compliant; doesn't work in IE
	} catch(ex) {
		combo.add(option, combo.selectedIndex); // IE only
	}
	
	var hidden = document.getElementById("tagInputHidden");
	var newtok = '';
	var tok = hidden.value.split(" ");
	var firstTok = true;
	for(var i=0;i<tok.length;i++) {
		if(tok[i]!=tagid) {
			if(firstTok==true) {
				firstTok = false;
				newtok = tok[i];
			} else {
				newtok += ' '+tok[i];
			}
		}
	}
	hidden.value = newtok;
	
	tagdiv.removeChild(document.getElementById("txt"+tagid));
	tagdiv.removeChild(document.getElementById("lnk"+tagid));
}

function agregar_tag(tagid){
	var tagdiv=document.getElementById("tagdiv");
	var combo=document.getElementById("tagcombo");
	
	var oOptionSelected;
	var oOptionIndex;
	if (tagid!='') {
		var j=0;
		for (j=0; j<combo.length; j++) {
			if(combo.options[j].value==tagid) {
				oOptionSelected = combo.options[j];
				oOptionIndex = j;
				break;
			}
		}
	} else {
		oOptionSelected = combo.options[combo.selectedIndex];
		oOptionIndex = combo.selectedIndex;
	}

	var i;
	for (i=0; i<combo.length; i++) {
		if(combo.options[i].value==oOptionSelected.value) {
			combo.remove(i);
			break;
		}
	}
	
	if(oOptionSelected!='') {
		var texto = document.createElement("span");
		texto.id = "txt"+oOptionSelected.value;
		texto.innerHTML = oOptionSelected.text;

		var remover = document.createElement("a");
		var pickText=document.createTextNode('(quitar)');
		remover.id = "lnk"+oOptionSelected.value;
		remover.appendChild(pickText);
		remover.setAttribute("href","javascript:quitar_tag("+oOptionSelected.value+","+oOptionIndex+",'"+oOptionSelected.text+"');");

		var space = document.createTextNode(" ");

		var hidden = document.getElementById("tagInputHidden");
		if(hidden.value=='') {
			hidden.value = oOptionSelected.value;
		} else {
			hidden.value = hidden.value + ' ' + oOptionSelected.value;
		}
		
		tagdiv.appendChild(texto);
		tagdiv.appendChild(remover);
		tagdiv.appendChild(space);
		tagdiv.appendChild(hidden);
		if(combo.length == 1) {
			combo.style.visibility = "hidden";
		}
	}
}
</script>

<table border="0" cellpadding="0" cellspacing="4" width="100%">
	<tr>
		<td height="50">
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr>
					<td align="center" class="titulo">ALTA DE NOTICIA</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td width="100%">
			<form name="frm_alta" method="post" action="noticias/operations.php" onSubmit="javascript:return validate_empty(document.frm_alta.title);">
				<input type="hidden" name="noticia_id" value="<?php echo $_REQUEST['noticia_id']; ?>">
				<input type="hidden" name="axn" value="<?php echo $_REQUEST['axn']; ?>">
				<input type="hidden" id="tagInputHidden" name="tagInputHidden" value="">
				<table border="0" cellpadding="0" cellspacing="4">
					<tr>
						<td align="right">Titulo</td>
						<td><input class="input" type="text" name="title" value="<?php echo $newsResult['title']; ?>" style="width:900px;"></td>
					</tr>
					<?php
					$tagsCountString = "select count(*) tagsQty from tags where status='A'";
					$tagsCountQuery = dbSelect($tagsCountString) or die("Error en ".$tagsCountString." ".mysql_error());
					if ($tagsCountResult = mysql_fetch_array($tagsCountQuery)) {
						$tagsQty = $tagsCountResult['tagsQty'];
					}
					if($tagsQty>0) {?>
						<tr>
							<td align="right">Tags</td>
							<td>
								<div id="tagdiv">
									<select class="input" name="tags" id="tagcombo" onChange="javascript:agregar_tag('');">
										<option value=""></option>
									<?php
										$tagsString = "select tag_id,name from tags where status='A' order by name";
										$tagsQuery = dbSelect($tagsString) or die("Error en select ".$tagsString." ".mysql_error());;
										while($tagsResult = mysql_fetch_array($tagsQuery)) {
											echo "<option value='".$tagsResult['tag_id']."'>".$tagsResult['name']."</option>";
										}
									?>
									</select>
									<?php 
									if(isset($_REQUEST['axn']) && $_REQUEST['axn']=='update_news') {
										$tagsNoticiasString = "select tag_id from tags_noticias where noticia_id=".$_REQUEST['noticia_id'];
										$tagsNoticiasQuery = dbSelect($tagsNoticiasString) or die("Error en select ".$tagsNoticiasString." ".mysql_error());;
										$isFirst = true;
										while($tagsNoticiasResult = mysql_fetch_array($tagsNoticiasQuery)) {
											if($isFirst) {
												echo "<script language='javascript'>";
												$isFirst = false;
											}
											echo "agregar_tag(".$tagsNoticiasResult['tag_id'].");";
										}
										if(!$isFirst) {
											echo "</script>";
										}
									}
									?>
								</div>
							</td>
						</tr>
					<?php } ?>
					<tr>
						<td align="right"></td>
						<td height="300">
							<?php
								$oFCKeditor = new FCKeditor('FCKeditor1') ;
								$oFCKeditor->BasePath = 'fckeditor/' ;
								$oFCKeditor->Height	= 600;
								$oFCKeditor->Width	= 900;
								$oFCKeditor->Value = strip_tags($newsResult['body'],'<p><br>');
								$oFCKeditor->Create() ;
							?>
						</td>
					</tr>
					<tr>
						<td align="right">Status</td>
						<td>
							<select name="status" class="input">
								<option value="A" <?php echo (isset($newsResult['status']) && $newsResult['status']=='A')?'selected':''; ?> >Activo</option>
								<option value="I" <?php echo (isset($newsResult['status']) && $newsResult['status']=='I')?'selected':''; ?> >Inactivo</option>
							</select>
						</td>
					</tr>
					<tr>
						<td height="10"></td>
					</tr>
					<tr>
						<td align="right"></td>
						<td align="center">
							<input class="input" type="submit" name="guardar" value="Guardar">
							<input class="input" type="button" name="cancelar" value="Cancelar" onClick="javascript:window.location.href='index.php?admin=noticias';">
						</td>
					</tr>
				</table>
			</form>
		</td>
	</tr>
</table>